package com.aiti.myproject.client.widgets;

import com.aiti.myproject.client.events.GalleryUpdatedEvent;
import com.aiti.myproject.client.events.GalleryUpdatedEventHandler;
import com.aiti.myproject.client.service.UserImageService;
import com.aiti.myproject.client.service.UserImageServiceAsync;
import com.aiti.myproject.shared.LoginInfo;
import com.aiti.myproject.shared.UploadImage;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.event.shared.HasHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;

public class UploadPhoto extends Composite implements HasHandlers{

	private static UploadPhotoUiBinder uiBinder = GWT
			.create(UploadPhotoUiBinder.class);
	UserImageServiceAsync imageService = GWT.create(UserImageService.class);
	private HandlerManager handlerManager;
	LoginInfo loginInfo;
	
	@UiField
	Button uploadButton;

	@UiField
	FileUpload fieldUpload;

	@UiField
	FormPanel uploadForm;

	interface UploadPhotoUiBinder extends UiBinder<Widget, UploadPhoto> {
	}

	public UploadPhoto(final LoginInfo loginInfo) {
		initWidget(uiBinder.createAndBindUi(this));
		handlerManager = new HandlerManager(this);
		this.loginInfo = loginInfo;
		uploadButton.setText("Upload");
		fieldUpload.setName("image");
		
		startNewBlobstoreSession();
		uploadForm.addSubmitCompleteHandler(new SubmitCompleteHandler() {
			
			@Override
			public void onSubmitComplete(SubmitCompleteEvent event) {
				// Test
				uploadForm.reset();
				startNewBlobstoreSession();
				String key = event.getResults();
				imageService.get(key, new AsyncCallback<UploadImage>() {
					
					@Override
					public void onSuccess(UploadImage result) {
						ImageOverlay imageOverlay = new ImageOverlay(result, loginInfo);
						GalleryUpdatedEvent event = new GalleryUpdatedEvent();
						fireEvent(event);
						final PopupPanel popupPanel = new PopupPanel();
						popupPanel.setAnimationEnabled(true);
						popupPanel.setAutoHideEnabled(true);
						popupPanel.setGlassEnabled(true);
						popupPanel.setWidget(imageOverlay);
						popupPanel.show();
						popupPanel.center();
					}
					
					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Upload false");
					}
				});
			}
		});
	}

	private void startNewBlobstoreSession() {
		imageService.getBlobstoreUploadUrl(new AsyncCallback<String>() {

			@Override
			public void onSuccess(String result) {
				uploadForm.setAction(result);
				uploadForm.setEncoding(FormPanel.ENCODING_MULTIPART);
				uploadForm.setMethod(FormPanel.METHOD_POST);
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Upload photo onFailure: " + caught.getMessage());
			}
		});
	}

	@UiHandler("uploadButton")
	void onClick(ClickEvent e) {
		uploadForm.submit();
	}
	
	@Override
	public void fireEvent(GwtEvent<?> event) {
		handlerManager.fireEvent(event);
	}

	public HandlerRegistration addGalleryUpdatedEventHandler(
			GalleryUpdatedEventHandler handler) {
		return handlerManager.addHandler(GalleryUpdatedEvent.TYPE, handler);
	}

}
